<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>获奖信息查询</title>
    @include('common-css')
    <style>
        .input_box {
            width: 100%;
            height: 40px;
            margin-top: 15px;
            margin-bottom: 15px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .input_label {
            width: 120px;
            box-sizing: border-box;
        }

        .application-list {
            margin-top: 20px;
        }

        .phase-tag {
            color: #1890ff;
            border: 1px solid #91d5ff;
            border-radius: 3px;
            background: #e6f7ff;
            font-size: 12px;
            padding: 2px 3px;
        }

        .ant-table-tbody > tr > td, .ant-table-thead > tr > th {
            vertical-align: middle;
            line-height: 25px;
        }

        .recall_reason {
            color: red
        }
    </style>
</head>
<body>
<div id="app">

</div>
</body>
</html>

<script id="appTemplate" type="x-template">
    <a-locale-provider :locale="antdLocale">
        <div class="container">
            @include('home.header')

            <main>
                <div class="common_container">

                    @include('home.aside', ['selectedItem' => 'won-awards'])

                    <div class="common_content">
                        <div class="common_box">


                            <a-table class="application-list"
                                     rowKey="id"
                                     :columns="applicationListColumns"
                                     :dataSource="applications">
                                <span slot="award_name" slot-scope="text, record">
                                    @{{ findAwardInfoById(record.award_id).name }}
                                </span>
                                <span slot="project_name" slot-scope="text, record">
                                    @{{ record.project_name || '-' }}
                                </span>
                                <span slot="prof_category" slot-scope="text, record">
                                    <template v-if="record.award_id == 1">
                                        <a-select style="width: 140px;" size="small"
                                                  :disabled="!canProjectSubmitFormalReview(record)"
                                                  v-model="record.prof_category"
                                                  @change="handleProfCategoryChange(record, $event)">
                                        <a-select-option v-for="category in profCategories"
                                                         :value="category"
                                                         :key="category">@{{ category }}
                                        </a-select-option>
                                    </a-select>
                                    </template>
                                    <template v-else>无</template>
                                </span>
                                <span slot="nominators" slot-scope="text, record">
                                    <template v-if="findAwardInfoById(record.award_id).nomination_required">
                                        <p v-for="nominatorId in record.nominator_ids">@{{ getNominatorById(nominatorId).name }}</p>
                                    </template>
                                    <template v-else>-</template>
                                </span>
                                <span slot="status" slot-scope="text, record">
                                        <span v-if="record.award_status == 1"><a-tag color="blue">@{{ record.award }}</a-tag></span>
                                        <span v-else><a-tag color="red">未获奖</a-tag></span>

                                <div v-if="record.status_id == 'B1.2'" class="recall_reason">原因: @{{ record.status_remarks }}</div>

                            </span>


                            </a-table>

                        </div>
                    </div>
                </div>
            </main>


        </div>
    </a-locale-provider>
</script>

@include('common-js')

<script>
    const applicationListColumns = [{
        width: 40,
        title: '序号',
        dataIndex: 'num'
    }, {
        width: 80,
        title: '奖项',
        dataIndex: 'award_name',
        scopedSlots: {
            customRender: 'award_name'
        }
    }, {
        width: 80,
        title: '项目名称',
        dataIndex: 'project_name',
        scopedSlots: {
            customRender: 'project_name'
        }
    },
        {
            width: 100,
            title: '专业类别',
            dataIndex: 'prof_category',
            scopedSlots: {
                customRender: 'prof_category'
            }
        }, {
            width: 100,
            title: '获奖结果',
            dataIndex: 'status',
            scopedSlots: {
                customRender: 'status'
            }
        }];

    var awardsInfo = {!! json_encode(array_values(config('vars.awards')), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
    var nominators = {!! json_encode($nominators, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
    var applications = {!! json_encode($applications, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
    console.log(applications);
    var applicationOpenAwardIds = {!! json_encode($applicationOpenAwardIds) !!};

    function processApplicationsData(applications) {
        var n = 1
        applications.forEach(item => {
            item.num = n
            n += 1
        })
    }

    processApplicationsData(applications)

    Vue.prototype.$axios = axios;
    var app = new Vue({
        el: '#app',
        template: '#appTemplate',
        data: {
            profCategories,
            //是否开启申报
            DeclarePhaseStatus: {{$DeclarePhaseStatus ? 'true' : 'false'}},
            //是否结束形式审查
            AtPhaseFormalReview:{{$AtPhaseFormalReview ? 'true' : 'false'}},
            applicationOpenAwardIds,
            chooseAwardToApplyDialogVisible: false,
            chooseNominatorsDialogVisible: false,
            newApplicationAwardId: applicationOpenAwardIds.length ? applicationOpenAwardIds[0] : null,
            newApplicationNominatorId1: 0,
            newApplicationNominatorId2: 0,
            newApplicationNominatorId3: 0,
            newApplicationNominatorId4: 0,
            newApplicationNominatorId5: 0,
            applications: applications,
            nominators: nominators,
            submittingApplicationForm: false,
            deletingApplicationForm: false,
            awardsInfo: awardsInfo,
            applicationListColumns: applicationListColumns,
            changePasswordDialogVisible: false,
            passwordInfo: {
                password: '',
                passwordConfirm: ''
            },
            antdLocale: antd.locales.zh_CN
        },
        computed: {
            newApplicationAwardInfo() {
                return this.findAwardInfoById(this.newApplicationAwardId)
            },
            nominatorGroupNames() {
                var result = []
                this.nominators.forEach(item => {
                    if (!result.includes(item.group_name)) {
                        result.push(item.group_name)
                    }
                })
                return result
            }
        },
        mounted() {
        },
        methods: {
            changeApplicationStatus(application, statusId) {
                console.log('application', application)
                this.submittingApplicationForm = true
                antd.message.loading('正在处理，请稍候', 0)
                axios.post('/api/award/change-application-status', {
                    application_id: application.id,
                    status_id: statusId
                }).then(res => {
                    antd.message.destroy()
                    var data = res.data
                    if (data.ok) {
                        antd.message.success('操作成功')
                        this.reloadData()
                    } else {
                        antd.message.warn(data.message, 3)
                    }
                    this.submittingApplicationForm = false
                })
            },
            downloadAwardDoc(path) {
                window.open(path)
            },
            printAward(id) {
                window.open('/apply/show-print?award_id=' + id)
            },
            getNominatorById(id) {
                return this.nominators.filter(item => {
                    return item.id == id
                })[0]
            },
            getNominatorsByGroup(nominatorGroup) {
                return this.nominators.filter(item => {
                    return item.group_name == nominatorGroup
                })
            },
            handleSubmitApplicationForm(application) {
                axios.get('/api/common/get-workflow-phases').then(res => {
                    let declare = res.data.phases[0]
                    var applicationPhaseEnded = application.award_id == 1 && (declare.has_begun != 1 || declare.has_ended != 0)
                    if (applicationPhaseEnded && application.status_id !== 'B1.2') {
                        warn('申报已截止,无法执行该操作')
                        setTimeout(function () {
                            window.location.reload()
                        }, 1200)
                    } else {

                        console.log('application', application)
                        this.submittingApplicationForm = true
                        antd.message.loading('正在处理，请稍候', 0)
                        axios.post('/api/award/applicant-submit-application-form', {
                            application_id: application.id
                        }).then(res => {
                            antd.message.destroy()
                            console.log(res)
                            var data = res.data
                            if (data.ok) {
                                antd.message.success('提交成功')
                                this.reloadData()
                            } else {
                                antd.message.warn(data.message, 3)
                            }
                            this.submittingApplicationForm = false
                        })

                    }
                })
            },
            handleDeleteProject(project) {
                console.log('project', project)
                this.deletingApplicationForm = true
                antd.message.loading('正在处理，请稍候', 0)
                axios.post('/api/award/delete-application', {
                    application_id: project.id
                }).then(res => {
                    antd.message.destroy()
                    console.log(res)
                    var data = res.data
                    if (data.ok) {
                        antd.message.success('删除成功')
                        this.reloadData()
                    } else {
                        antd.message.warn(data.message, 3)
                    }
                    this.deletingApplicationForm = false
                })
            },
            showChooseNominatorsDialog() {
                this.chooseNominatorsDialogVisible = true
            },
            handleChooseNominatorsOk() {
                console.log('newApplicationAwardId', this.newApplicationAwardId)
                var nominatorIds = [this.newApplicationNominatorId1, this.newApplicationNominatorId2, this.newApplicationNominatorId3, this.newApplicationNominatorId4, this.newApplicationNominatorId5]
                nominatorIds = nominatorIds.filter(item => item != 0)
                var awardId = this.newApplicationAwardId
                var awardInfo = this.newApplicationAwardInfo
                console.log('nominatorIds', nominatorIds)
                if (nominatorIds.length == 0) {
                    antd.message.warn('{{ config('vars.strings.no_nominator_selected') }}')
                    return
                }
                this.submitNewApplication(awardId, nominatorIds)
                this.chooseNominatorsDialogVisible = false
            },
            handleChooseAwardToApplyOk() {
                console.log('newApplicationAwardId', this.newApplicationAwardId)
                this.chooseAwardToApplyDialogVisible = false
                var awardId = this.newApplicationAwardId
                var awardInfo = this.newApplicationAwardInfo
                if (awardInfo.nomination_required) {
                    this.showChooseNominatorsDialog()
                } else {
                    this.submitNewApplication(awardId, [])
                }
            },
            submitNewApplication(awardId, nominatorIds) {
                antd.message.loading('正在处理，请稍候', 0)
                axios.post('/api/award/create-application', {
                    award_id: awardId,
                    nominator_ids: nominatorIds
                }).then(res => {
                    console.log(res)
                    var data = res.data
                    if (data.ok) {
                        antd.message.success('创建成功，请在线填写申报书')
                        this.reloadData()
                    } else {
                        antd.message.destroy()
                        antd.message.warn(data.message, 3)
                    }
                })
            },
            showChooseAwardToApplyDialog() {
                this.chooseAwardToApplyDialogVisible = true
                // axios.get('/api/common/get-workflow-phases').then(res => {
                //     let declare = res.data.phases[0]
                //     if (declare.has_begun != 1 || declare.has_ended != 0) {
                //         warn('申报已截止,无法执行该操作')
                //         setTimeout(function () {
                //             window.location.reload()
                //         },1200)
                //     } else {
                //     }
                //
                // })
            },
            canProjectSubmitFormalReview(project) {
                if (this.applicationOpenAwardIds.includes(project.award_id)
                    && (project.status_id == 'B1' || project.status_id == 'B1.2')) {
                    return true
                } else if (project.status_id == 'B1.2') {
                    return true
                }
                return false
            },
            findAwardInfoById(id) {
                return this.awardsInfo.filter(item => {
                    return item.id == id
                })[0]
            },
            reloadData() {
                axios.post('/api/award/get-applications')
                    .then(res => {
                        antd.message.destroy()
                        var applications = res.data
                        console.log(applications)
                        processApplicationsData(applications)
                        this.applications = applications
                    })
            },
            handleHeaderDropdownItemClick: handleHeaderDropdownItemClick,
            handleChangePasswordOk: handleChangePassword,
            handleOrgBusinessLicensePhotoBeforeUpload(file) {
                console.log('handleOrgBusinessLicensePhotoBeforeUpload', file)
                var filename = file.name.toLowerCase()
                if (filename.endsWith('.jpg') || filename.endsWith('.jpeg') || filename.endsWith('.png')) {
                    antd.message.loading('正在上传...', 0)
                    return true
                } else {
                    antd.message.warn('不允许上传该文件格式')
                    return false
                }
            },
            handleOrgBusinessLicensePhotoUploadChange(event) {
                console.log('handleOrgBusinessLicensePhotoUploadChange', event)
                if (event.file.status == 'done') {
                    antd.message.destroy()
                    if (event.file.response.ok) {
                        let photo_path = event.file.response.url
                        axios.post('/api/auth/reupload-org-bizlic-Photo', {photo_path})
                            .then(res => {
                                antd.message.success('上传成功')
                                history.go(0)
                            })
                    } else {
                        antd.message.warn('上传失败')
                    }
                }
            },
            handleProfCategoryChange(record, value) {
                axios.post('/api/apply/update-project-attrs', {
                    project_id: record.id,
                    attrs: {
                        prof_category: value
                    }
                }).then(response => {
                    if (response.data.ok) {
                        success('修改成功')
                    } else {
                        error('修改失败，请重试')
                    }
                })
            },
        }
    })
</script>
